package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2023-10-27
 * Time:15:16
 * Text：
 */
//响应
@RequestMapping("/return")
@Controller
public class ReturnController {

//    返回网站
    @RequestMapping("/index")
    public String returnindex(){
        return "/index.html";
    }

    //返回数据
    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData(){
        return "数据";
    }

//返回网页的内容
//    当接口返回的是String，content-Type是text/html格式
    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml(){
        return "<h1>我爱Java</h1>";
    }

//    接口返回是对象时，返回json格式
//@RequestMapping(value="/returnJson",produces="application/json;charset=utf8")
    @ResponseBody
    @RequestMapping("/returnJson")
    public Person returnJson(){
        Person person=new Person();
        person.setAge(22);
        person.setName("zhangsan");
        person.setSex("男");
        return person;
    }

//当返回的是Map时，content-Type自动设置为application/json格式
    @ResponseBody
    @RequestMapping("/returnMap")
    public Map<String,String> returnMap(){
        Map<String,String> kv = new HashMap<>();
        kv.put("k1","v1");
        kv.put("k2","v2");
        kv.put("k3","v3");
        return kv;
    }



    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(401);//通常表示没有登录
        return "设置状态码";
    }


}
